package cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing;

import cn.edu.neu.softlab633.influencemaximization.sy.Index.NetworkIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.Index.TopicIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.bean.Index;
import cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing.InfluenceCal.InfluenceCal;

import java.io.*;
import java.util.*;

/**
 * 知乎数据预处理
 * Created by Jason on 2017/4/19.
 */
public class ZhihuData implements Preprocessing {

    @Override
    public void topicPreprocessing() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("data/zhihu_topic.csv"));
        Set<String> zhihutopic = new HashSet<>();
        String topic = br.readLine();
        while (topic != null) {
            String[] tt = topic.split(",");
            if (tt.length == 2) {
                zhihutopic.add((topic.split(","))[1]);
            } else {
                System.out.print(tt.length + " ");
                System.out.println(topic);
            }
            topic = br.readLine();
        }
        System.out.println(zhihutopic.size());
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/zhihu_topic_new.txt"));
        for (String s : zhihutopic) {
            bw.write(s);
            bw.newLine();
        }
        br.close();
        bw.close();
    }

    @Override
    public void relationPreprocessing() throws Exception {
        BufferedReader weiborelation = new BufferedReader(new FileReader("data/WZ/weibouserrealation.csv"));
        BufferedWriter weiborelation_topic = new BufferedWriter(new FileWriter("data/WZ/weiborelation_topic.csv"));
        Map<Long, Double[]> weibotopic = TopicIndex.getTopicIndex("data/WZ/weibouser_topic.csv");
        Map<Integer, ArrayList<Integer>> idf = NetworkIndex.getReverseIndex("data/WZ/weibouserrealation.csv");
        String line = weiborelation.readLine();
        line = weiborelation.readLine();
        while (line != null) {
            long id1 = Long.valueOf(line.split(",")[0]);
            long id2 = Long.valueOf(line.split(",")[1]);
            String weight = InfluenceCal.calEdgeWeight(id1, id2, weibotopic, idf);
            weiborelation_topic.write(line + "," + weight);
            weiborelation_topic.newLine();
            line = weiborelation.readLine();
        }
        weiborelation_topic.close();
        weiborelation.close();
    }

    public void weiboTopic() throws IOException {
        BufferedReader weiborelationbe = new BufferedReader(new FileReader("data/WZ/weibouserrealation.csv"));
        BufferedReader weibozhihu = new BufferedReader(new FileReader("data/WZ/z_w_topic.csv"));
        BufferedWriter weibousertopic = new BufferedWriter(new FileWriter("data/WZ/weibouser_topic.csv"));
        Set<Long> weibouser = new HashSet<>();
        Map<Long, Integer> wz = new HashMap<>();
        String line = weibozhihu.readLine();
        while (line != null) {
            long weibo = Long.valueOf(line.split(",")[1]);
            int zhihu = Integer.valueOf(line.split(",")[0]);
            wz.put(weibo, zhihu);
            line = weibozhihu.readLine();
        }
        Map<Integer, Double[]> zhihutopic = TopicIndex.getTopicIndex("data/WZ/zhihuuser_topic.csv");
        line = weiborelationbe.readLine();
        line = weiborelationbe.readLine();
        while (line != null) {
            long id1 = Long.valueOf(line.split(",")[0]);
            long id2 = Long.valueOf(line.split(",")[1]);
            weibouser.add(id1);
            weibouser.add(id2);
            line = weiborelationbe.readLine();
        }
        Iterator<Long> iterator = weibouser.iterator();
        while (iterator.hasNext()) {
            Long next = iterator.next();
            Double topic = 0.0;
            if (wz.containsKey(next)) {
                topic = zhihutopic.get(wz.get(next))[0];
            } else {
                topic = Math.random();
            }
            weibousertopic.write(next + "," + topic);
            weibousertopic.newLine();
        }
        weibozhihu.close();
        weibousertopic.close();
        weiborelationbe.close();
    }

    public void weiboNext() throws IOException {
        BufferedReader weiborelation = new BufferedReader(new FileReader("data/WZ/weiborelation_topic.csv"));
        BufferedReader wz = new BufferedReader(new FileReader("data/WZ/z_w_topic.csv"));
        BufferedWriter weiborelationbw = new BufferedWriter(new FileWriter("data/WZ/weiborelation_topic_new.csv"));
        BufferedWriter wzbw = new BufferedWriter(new FileWriter("data/WZ/z_w_topic_new.csv"));
        Map<Long, Integer> nametonum = new HashMap<>();
        Integer num = 0;
        String line = weiborelation.readLine();
        while (line != null) {
            long id1 = Long.valueOf(line.split(",")[0]);
            long id2 = Long.valueOf(line.split(",")[1]);
            if (!nametonum.containsKey(id1)) {
                nametonum.put(id1, num);
                num++;
            }
            if (!nametonum.containsKey(id2)) {
                nametonum.put(id2, num);
                num++;
            }
            line = weiborelation.readLine();
        }
        weiborelation.close();
        BufferedReader relation = new BufferedReader(new FileReader("data/WZ/weiborelation_topic.csv"));
        line = relation.readLine();
        while (line != null) {
            long id1 = Long.valueOf(line.split(",")[0]);
            long id2 = Long.valueOf(line.split(",")[1]);
            String topic = line.split(",")[2];
            int id11 = nametonum.get(id1);
            int id22 = nametonum.get(id2);
            weiborelationbw.write(id11 + "," + id22 + "," + topic);
            weiborelationbw.newLine();
            line = relation.readLine();
        }
        line = wz.readLine();
        while (line != null) {
            String zid = line.split(",")[0];
            long id = Long.valueOf(line.split(",")[1]);
            String topic = line.split(",")[2];
            int tt = nametonum.get(id);
            wzbw.write(zid + "," + tt + "," + topic);
            wzbw.newLine();
            line = wz.readLine();
        }
        relation.close();
        wzbw.close();
        weiborelationbw.close();
        wz.close();
        weiborelation.close();
    }
}
